package com.IO;

import java.io.*;
import java.util.Properties;
import java.util.Set;

/***
 * @ClassName: PropertiesDemo
 * @Description:
 * @Auther: 送一一块二向箔
 * @Date: 14:55 2022/2/7
 * @version : V1.0
 */
public class PropertiesDemo {

    public static void main(String[] args) {
        popTest();
        popStreamStore();
        popStreamLoad();
    }

    public static void popTest() {
        //创建
        Properties properties = new Properties();
        //添加数据
        properties.setProperty("name", "张三");
        properties.setProperty("age", "3");
        System.out.println(properties.toString());

        //遍历
        Set<String> strings = properties.stringPropertyNames();
        for (String string : strings) {
            System.out.println(string + "=======" + properties.getProperty(string));
        }
    }

    //流有关的方法
    public static void popStreamList() {
        try {
            Properties properties = new Properties();
            properties.setProperty("name", "张三");
            properties.setProperty("age", "3");
            PrintWriter printWriter = new PrintWriter("d:\\print.txt");
            //把properties中的文件输出到文件中
            properties.list(printWriter);
            printWriter.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }

    //Store 保存
    public static void popStreamStore() {
        try {
            Properties properties = new Properties();
            properties.setProperty("name", "张三");
            properties.setProperty("age", "3");
            FileOutputStream fos = new FileOutputStream("d:\\store.properties");
            //把properties中的文件输出到文件中
            properties.store(fos, "备注");
            fos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //Load方法保存
    public static void popStreamLoad() {
        try {
            Properties properties = new Properties();
            FileInputStream fis = new FileInputStream("d:\\store.properties");
            //读取文件中的内容到Properties中
            properties.load(fis);
            System.out.println(properties.toString());
            fis.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
